//
//  ActivityPopupManager.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/14.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import Foundation

class ActivityPopupManager {
    static let shared = ActivityPopupManager()
    
    private init() {}
    
    /// 获取活动弹窗数据
    /// - Parameter completion: 回调，返回弹窗模型
    func getActivityPopup(completion: @escaping (ActivityPopupModel?) -> Void) {
        // 实际的API调用应该是这样的：
        YLRequest.request(ActivityAPI.getPopup) { result in
            switch result {
            case .success(let res):
                let dataDictionary = res.dataDictionary
                if let model = JsonUtil.modelFromDictionary(dataDictionary, ActivityPopupModel.self) {
                    if (model.video_url.isEmpty) {
                        model.type = 1;
                    } else {
                        model.type = 2;
                    }
                    completion(model)
                } else {
                    completion(nil)
                }
            case .failure:
                completion(nil)
            }
        }
        
    }
    
    /// 检查是否应该显示弹窗
    /// - Parameter model: 弹窗模型
    /// - Returns: 是否显示
    func shouldShowPopup(_ model: ActivityPopupModel) -> Bool {
        guard model.status else { return false }
        
        // 检查显示次数限制
        if model.currentShowCount >= model.showLimit {
            return false
        }
        
        // 检查时间范围
        let now = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        if !model.startTime.isEmpty {
            if let startDate = dateFormatter.date(from: model.startTime) {
                if now < startDate {
                    return false
                }
            }
        }
        
        if !model.endTime.isEmpty {
            if let endDate = dateFormatter.date(from: model.endTime) {
                if now > endDate {
                    return false
                }
            }
        }
        
        return true
    }
    
    /// 记录弹窗显示次数
    /// - Parameter model: 弹窗模型
    func recordPopupShow(_ model: ActivityPopupModel) {
        model.currentShowCount += 1
        model.hasShown = true
        
        // 可以在这里保存到本地存储
        savePopupRecord(model)
    }
    
    /// 保存弹窗记录到本地
    /// - Parameter model: 弹窗模型
    private func savePopupRecord(_ model: ActivityPopupModel) {
        let key = "activity_popup_\(model.popupId)"
        let record = [
            "popupId": model.popupId,
            "showCount": model.currentShowCount,
            "lastShowTime": Date().timeIntervalSince1970
        ] as [String: Any]
        
        UserDefaults.standard.set(record, forKey: key)
    }
    
    /// 获取本地弹窗记录
    /// - Parameter popupId: 弹窗ID
    /// - Returns: 记录字典
    func getPopupRecord(_ popupId: String) -> [String: Any]? {
        let key = "activity_popup_\(popupId)"
        return UserDefaults.standard.dictionary(forKey: key)
    }
} 
